
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

using Swaf.BizObj;
using Swaf.Container;

namespace Swaf.Gui
{
	[Serializable]
	public class WidgetOfflineState
	{
		protected IMap m_state;
		protected List<string> m_parentBizProcIds = new List<string>();
		public string Id;
		public string FullyQualifiedClassName;
		public BizProcWidgetType BizProcWidgetType;

		public WidgetOfflineState(string widgetId, string widgetFullyQualifiedClassName)
		{
			Id = widgetId;
			FullyQualifiedClassName = widgetFullyQualifiedClassName;
		}

		public List<string> ParentBizProcIds { get { return m_parentBizProcIds; } }

		public void setParentBizProcIds(string parentBizProcIds)
		{
			m_parentBizProcIds = new List<string>();
			if (!Cmn.IsEmpty(parentBizProcIds))
			{
				string[] bizProcIds = parentBizProcIds.Split(',');
				ParentBizProcIds.AddRange(bizProcIds);
			}
		}

		public IMap State
		{
			get
			{
				if (m_state == null)
					m_state = new FlexiMap();
				return m_state;
			}
		}

		public void saveControlState(object ctl)
		{
			PropertyInfo[] pis = ctl.GetType().GetProperties();
			foreach (PropertyInfo pi in pis)
			{
				try
				{
					if(!pi.DeclaringType.FullName.StartsWith("System.") && pi.CanRead && pi.CanWrite)
						State[pi.Name] = pi.GetValue(ctl, null);
				}
				catch (Exception)
				{
				}
			}
		}

		public void getControlState(object ctl)
		{
			PropertyInfo[] pis = ctl.GetType().GetProperties();
			foreach (PropertyInfo pi in pis)
			{
				try
				{
					if (State.containsField(pi.Name) && pi.CanRead && pi.CanWrite)
						pi.SetValue(ctl, State.get(pi.Name), null);
				}
				catch (Exception)
				{
				}
			}
		}

	}
}
